// pages/add1/add1.js
var util = require('../../utils/util.js')
const app=getApp();
Page({
    gotoPage2:function(e){
        var that = this;
        wx.switchTab({
          url: '/pages/community/community',
        })
    },
    //获取地理位置
    location:function(){
      var that = this;
      wx.getLocation({
        type:'wgs84',
        success:function(res){
          console.log(res)
          var latitude = res.latitude
          var longitude = res.longitude
          wx.chooseLocation({
            latitude:latitude,
            longitude:longitude,
            success:function(res){
              console.log(res)
              that.setData({
                Wcity:res.address+' '+res.name
              })
              console.log(that.data.Wcity)
            },
            fail:function(){
              console.log('获取地理位置失败!')
            }
          })
        },
        fail:function(){
          console.log('fail')
        }
      })
    },
    //添加图片
    chooseImage:function(){
      var that = this;
        wx.chooseImage({
          count: 12,
          sizeType:['original','compressed'],
          sourceType:['album','camera'],
          success(res){
            // console.log(res.tempFilePaths)
            const tempFilePaths = res.tempFilePaths
            that.setData({
              src:[...that.data.src,...res.tempFilePaths]
            })
          },
          fail(res){
            console.log('添加图片失败！')
          }
        })
    },
    //预览图片
    previewImage:function(e){
        var that = this
        var src = e.currentTarget.dataset.src
        wx.previewImage({
            current: src,
            urls:that.data.src
        })
    },
    //删除图片
    deleteImage:function(e){
      var that = this;
      var src = that.data.src
      var index = e.currentTarget.dataset.index  //获取当前长按图片下标
      wx.showModal({
        title:'提示',
        content:'确定要删除此图片吗？',
        success: function(res){
          if(res.confirm){
            src.splice(index,1);
            console.log('点击确定了')
          }else if(res.cancel){
            console.log('点击取消了')
            return false;
          }
          that.setData({
            src
          })
        }
      })
    },
  //获取数据
  formsubmit:function(e){
    var that = this;
    console.log(that.data.Wtag)
    //获取当前时间
    var dayTime = util.formatTime(new Date());
    console.log(dayTime)
    var time1 = dayTime.split('/')
    var time2 = time1[0]+'-'+time1[1]+'-'+time1[2]+' '
    var time3 = time2.split(':')
    var time4 = time3[0]+':'+time3[1]
    console.log(time4)
    that.setData({
      Wtime:time4
    })
    if(this.data.src.length === 0){
      wx.showModal({
        title:'提示',
        content:'你还没有上传图片呢~',
        success: function(res){
          if(res.confirm){
            console.log('点击确定了')
          }else if(res.cancel){
            console.log('点击取消了')
            return false;
          }
        }
      })
    }else if(e.detail.value.title === ''){
      wx.showModal({
        title:'提示',
        content:'你还没有填写标题呢~',
        success: function(res){
          if(res.confirm){
            console.log('点击确定了')
          }else if(res.cancel){
            console.log('点击取消了')
            return false;
          }
        }
      })
    }else if(e.detail.value.content === ''){
      console.log('输出')
      wx.showModal({
        title:'提示',
        content:'你还没有填写内容呢~',
        success: function(res){
          if(res.confirm){
            console.log('点击确定了')
          }else if(res.cancel){
            console.log('点击取消了')
            return false;
          }
        }
      })
    }else if(this.data.Wtag === ''){
      wx.showModal({
        title:'提示',
        content:'你还没有选择星球呢~',
        success: function(res){
          if(res.confirm){
            console.log('点击确定了')
          }else if(res.cancel){
            console.log('点击取消了')
            return false;
          }
        }
      })
    }else{
      wx.navigateTo({
        url: '/pages/add1/add1',
      })
      let myId = wx.getStorageSync('Uid')
      wx.request({
        url:`${app.globalData.url}/addWorks/addWorks`,
        data:{
          //获取标题数据
          Uid:myId,
          Wtitle:e.detail.value.title,
          //获取内容数据
          Wintro:e.detail.value.content,
          //获取选择星球数据
          Wtag:that.data.Wtag,
          Wimage:that.data.src,
          Wzan:0,
          Wtime:time4,
          Wcity:that.data.Wcity
        },
        method:'post',
        header:{
          'content-type':'application/json'
        },
        success(res){
            //注意that
            //清理图片，标题，星球选择
            that.setData({
              title:'',
              content:'',
              src:[],
              Wtag:'',
              Wtime:''
            })
        },
        fail:function(res){
          console.log('存储标题，内容，星球失败！')
        }
      })
    }
  },
  /**------------------------------------------------- */
  //选择星球
  change1: function(){
    this.setData({
        display1:'block',
        display2:'none',
        display3:'none',
        Wtag:'#汪汪星球',
        Uid:''
    })
  },
  change2: function(){
      var that = this;
      this.setData({
          display1:'none',
          display2:'block',
          display3:'none',
          Wtag:'#喵喵星球'
      })
  },
  change3: function(){
      var that = this;
      this.setData({
          display1:'none',
          display2:'none',
          display3:'block',
          Wtag:'#其他星球'
      })
  },
  //存储选择星球的数据
  /**-------------------------------------------- */
    /**
     * 页面的初始数据
     */
    data: {
      src:[],
      Wtag:'',
      Wtime:'',
      Uid:'',
      Wcity:'',
      display1:'block',
      display2:'none',
      display3:'none',
    }
})